/** 
 * @jsName: InsureOrderManaged.js 
 * @author Sean Cui
 * @date 2017-04-16 下午3:46:00 
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @version V1.0.0.0
 * @comment 该文件通过代码生成器生成，如果修改次文件后，重新生成时将覆盖修改内容，请修改生成模板或做好备份
 */

var InsureOrderManaged = function() {
	return {
		datatable : null,
		init : function() {
			$("#applyTimestime").datetimepicker({lang:'ch',timepicker:false, format:"Y-m-d"});
			$("#applyTimeetime").datetimepicker({lang:'ch',timepicker:false, format:"Y-m-d"});
			this.initSearch();
			this.initListener();
		},
		initSearch : function() {
			if (!jQuery().dataTable) {
				return;
			}
			var listTable = $("#InsureOrderListTable").dataTable({
				"sAjaxSource" : ctx + "/insureOrder/search?now=" + new Date().getTime(),
				/* 使用post方式 */
				"fnServerData" : function(sSource, aoData, fnCallback) {
					var postData = {
						page : 1,
						start : 0,
						rows : 10,
						params : {}
					};

					if($('#orderNo').val()){
						postData.params.orderNo = "%"+($('#orderNo').val()).replace(/\s/g,"%")+"%";
					}

					if($('#insureCompany').val()){
						postData.params.insureCompany = "%"+($('#insureCompany').val()).replace(/\s/g,"%")+"%";
					}

					if($('#elevatorNumStart').val()){
						postData.params.elevatorNumStart = $('#elevatorNumStart').val();
					}

					if($('#elevatorNumEnd').val()){
						postData.params.elevatorNumEnd = $('#elevatorNumEnd').val();
					}

					if($('#orderAmountStart').val()){
						postData.params.orderAmountStart = $('#orderAmountStart').val();
					}

					if($('#orderAmountEnd').val()){
						postData.params.orderAmountEnd = $('#orderAmountEnd').val();
					}

					
					if($('#searchTime').val()){
						var times = $('#searchTime').val().split(' - ');
						if(times.length == 2) {
							postData.params.applyTimestime = times[0];
							postData.params.applyTimeetime = times[1];
						}
					}
					
					if($('#state').val()){
						postData.params.state = $('#state').val();
					}



					// 参数
					for (var i = 0, size = aoData.length; i < size; i++) {
						if (aoData[i].name == 'iDisplayLength') {
							postData.rows = aoData[i].value;
						} else if (aoData[i].name == 'iDisplayStart') {
							postData.start = aoData[i].value;
						} else if (aoData[i].name == 'sEcho') {
							postData.page = aoData[i].value;
						}
					}

					$.ajax({
						"dataType" : 'json',
						"type" : "POST",
						"cache": false,
						"url" : sSource,
						"data" : postData,
						"success" : function(data, textStatus, jqXHR){
							fnCallback(data.data, textStatus, jqXHR);
						}
					});
				},
				"bAutoWidth":false,
				"aoColumnDefs" : [
						{
							"data" : "index",
							"aTargets" : [ 0 ]
						},
						{
							"data" : "insureCompany",
							"aTargets" : [ 1 ]
						},
						{
							"data" : "orderNo",
							"aTargets" : [ 2 ]
						},
						{
							"data" : "acceptUnit",
							"aTargets" : [ 3 ]
						},
						{
						"data" : "elevatorNum",
						"aTargets" : [ 4 ]
						},
					{
						"aTargets" : [ 5 ],
						"mRender" :function(data, type, full) {
							var contnet="";
							if(full.startTime!=null){
								contnet=contnet+new Date(full.startTime).format("yyyy-MM-dd");
							}
							contnet=contnet+" — ";
							if(full.endTime!=null){
								contnet=contnet+new Date(full.endTime).format("yyyy-MM-dd");
							}
							return contnet;
						}

					},

					{
						"data" : "orderAmount",
						"aTargets" : [ 6 ]
					},
					{
						"aTargets" : [ 7 ],
						"mRender" :function(data, type, full) {
							if(full.state==0){
								return "未审核";
							}else if(full.state==1){
								return "已审核(待付款)";
							}else if(full.state==3){
								return "已付款";
							}else if(full.state==4){
								return "生效";
							}else if(full.state==5){
								return "驳回";
							}else if(full.state==6){
								return "失效";
							}else{
								return "";
							}
						}
					},
					{
						"aTargets" : [ 8 ],
						"mRender" :function(data, type, full) {
							if(full.applyTime!=null){
								return new Date(full.applyTime).format("yyyy-MM-dd");
							}else{
								return "";
							}
						}
					},
						{
							"aTargets" : [ 9 ],
							"mRender" : function(data, type, full) {
								var btns =  '';
								if(full.state==0){
									btns +=  Permission.createPermElement('insureOrder:list:audit',' <button title="'+messages.public_button_audit+'" href="javascript:;" class=" btn btn-sm-table  btn-primary " name="audit" target_id="'+full.id+'">审核</button>');
								}
								btns +=  Permission.createPermElement('insureOrder:list:modify',' <button title="'+messages.public_button_edit+'" class="btn btn-sm-table btn-primary" name="edit" target_id="'+full.id+'">编辑</button>');
								btns +=  Permission.createPermElement('insureOrder:list:showele',' <button title="'+messages.public_button_showElevator+'" class="btn btn-sm-table btn-primary" name="showele" target_id="'+full.id+'">查看电梯</button>');
								btns +=  Permission.createPermElement('insureOrder:list:showorder',' <button title="'+messages.public_button_previewInsure+'" class="btn btn-sm-table btn-primary" name="showorder" target_id="'+full.id+'">预览保单</button>');
								return btns;
							}
						}
				]
			});
			this.datatable = listTable;
		},reSearch:function(){
        	this.datatable.fnDraw();
        },removeClass:function(){//去除Form表单的验证样式
        	$('.form-group','#actForm').removeClass('has-success')
            .removeClass('has-error');
    		 $('span').remove('.help-block');
        },initListener : function() {

			//审核投保单
			$('#InsureOrderListTable tbody').on('click', 'button[name="audit"]',function() {
				var id = $(this).attr("target_id");
				BootstrapDialog.show({
					type : BootstrapDialog.TYPE_INFO,
					title : messages.public_cue_message,
					message : "确认审核",
					buttons : [ {
						label : "不通过",
						cssClass: 'btn btn-sm btn-default',
						action : function(dialogItself) {
							InsureOrderManaged.sureAudit(id,0);
							dialogItself.close();
						}
					}, {
						label : "通过",
						cssClass : 'btn btn-sm btn-primary',
						action : function(dialogItself) {
							InsureOrderManaged.sureAudit(id,1);
							dialogItself.close();
						}
					} ]
				});
			});
			/*
			 * 预览投保单
			 */
			$("#show").click(function(e) {
				alert(33);
				// $("#actDetailForm #insureCompany").val($("#actForm #insureCompany").val());
				// $("#actDetailForm #insureLinkman").val($("#actForm #insureLinkman").val());
				// $("#actDetailForm #insurePhone").val($("#actForm #insurePhone").val());
				// $("#actDetailForm #usingUnit").val($("#actForm #usingUnit").val());
				// $("#actDetailForm #usingArea").val($("#actForm #usingArea").val());
				// $("#actDetailForm #usingAddress").val($("#actForm #usingAddress").val());
				// $("#actDetailForm #acceptUnit").val($("#actForm #acceptUnit").val());
				// // $("#actDetailForm #InsureOrderListTable tbody").html($("#actForm #InsureOrderListTable tbody").html());
				// $("#actDetailForm #elenum").html($("#actForm #elenum").html());
				// $("#actDetailForm #eleamont").html($("#actForm #eleamont").html());
				// InsureOrderApplyManaged.showedialoglevatortable();
				$("#showDetailDlg").modal("show");
				e.preventDefault();
			});
			/*
			 * 查询
			 */
			$("#searchBtn").click(function() {
				InsureOrderManaged.reSearch();
				return false;
			});

			/*
			 * 清除
			 */
			$("#clearBtn").click(function() {
				$("#searchForm")[0].reset();
				InsureOrderManaged.reSearch();
				return false;
			});

			/*
			 * 添加
			 */
			$("#addBtn").click(function() {
				InsureOrderManaged.removeClass();
				$("div[name=editDlg] h4.modal-title").html("新增");
				$("#actForm")[0].reset();
				$("#actForm").find("input[name='id']").val("");
				$("#editDlg").modal("show");
			});
			
			$("#actFormBtn").click(function(){
				var form1 = $('#actForm');
				form1.validate({
					rules : {
						orderNo:{
							maxlength : 25,
							required : true
						},
						acceptUnit:{
							maxlength : 25,
							required : true
						},
						insureCompany:{
							maxlength : 25,
							required : true
						},
						insureLinkman:{
							maxlength : 25,
							required : true
						},
						insurePhone:{
							maxlength : 25,
							required : true
						},
						usingUnit:{
							maxlength : 25,
							required : true
						},
						usingArea:{
							maxlength : 25,
							required : true
						},
						usingAddress:{
							maxlength : 25,
							required : true
						},
						elevatorNum:{
							maxlength : 25,
							required : true
						},
						orderAmount:{
							maxlength : 25,
							required : true
						},
						applyTime:{
							maxlength : 25,
							required : true
						},
						startTime:{
							maxlength : 25,
							required : true
						},
						endTime:{
							maxlength : 25,
							required : true
						},
						state:{
							maxlength : 25,
							required : true
						},
						version:{
							maxlength : 25,
							required : true
						}
					}
				});
				if(form1.valid()){
					var postData = $("form[name='actForm']").serializeJson();
					$.ajax({
						"dataType" : 'json',
						"cache": false,
						"type" : "POST",
						"data" : postData,
						"url" : ctx + "/insureOrder/edit?now="+new Date().getTime(),
						"success" : function(data) {
							if(data.type=='success'){
								$("#editDlg").modal("hide");
								BootstrapDialog.show({
									title : messages.public_cue_cue,
									message : messages.public_cue_saveOk,
									buttons : [ {
										label : messages.public_cue_sure,
										cssClass : 'btn btn-sm btn-primary',
										action : function(dialogItself) {
											InsureOrderManaged.reSearch();
											dialogItself.close();
										}
									} ]
								});
								
							}else{
								BootstrapDialog.show({
									type : BootstrapDialog.TYPE_WARNING,
									title : messages.public_cue_warning,
									message : data.message,
									buttons : [ {
										label : messages.public_cue_sure,
										cssClass : 'btn btn-sm btn-primary',
										action : function(dialogItself) {
											InsureOrderManaged.reSearch();
											dialogItself.close();
										}
									} ]
								});
							}
						}
					});
				}
			});
			
			/*
			 * 编辑
			 */
			$('#InsureOrderListTable tbody')
				.on('click', 'button[name="edit"]',function() {
					var id = $(this).attr("target_id");
					window.location.href = ctx+"/insureOrderApply/"+id+"/edit.html?type=1";
				});
			$('#InsureOrderListTable tbody')
                .on('click', 'button[name="del"]',function() {
                    var id = $(this).attr("target_id");
                    InsureOrderManaged.del(id);
                });

            $('#InsureOrderListTable tbody')
                .on('click', 'button[name="showele"]',function() {
                    var id = $(this).attr("target_id");
                    // console.log(id);
                    eleManaged.initData(id);
                    eleManaged.showdialog();
                });

            $('#InsureOrderListTable tbody')
                .on('click', 'button[name="showorder"]',function() {
                    var id = $(this).attr("target_id");
                    // console.log(id);
                    insureorderDetailManaged.initData(id);
                    insureorderDetailManaged.showdialog();
                });
		},
		/**
		 * 删除
		 */
		del : function(id) {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : messages.public_cue_warning,
				message : messages.public_cue_delSure,
				buttons : [ {
					label : messages.public_cue_close,
					cssClass: 'btn btn-sm btn-default',
					action : function(dialogItself) {
						dialogItself.close();
					}
				}, {
					label : messages.public_cue_sure,
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						InsureOrderManaged.sureDel(id);
						dialogItself.close();
					}
				} ]
			});
		},
		sureDel : function(id){

		},sureAudit:function(id,state){
			$.ajax({
				"dataType" : 'json',
				"cache": false,
				"type" : "POST",
				"data" :
					{
					id:id,
					state:state
					}
				, "url" : ctx + '/insureOrder/audit'+"?now="+new Date().getTime(),
				"success" : function(data, textStatus, jqXHR){
					if (data.type == 'success') {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : "操作成功",
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									InsureOrderManaged.reSearch();
									dialog.close();
								}
							} ]
						});
					} else {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : data.message,
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									dialog.close();
								}
							} ]
						});
					}
				},error:function(){
					alert(5);
				}
			});
		}
	};

}();

/*
 * Powered By [Code-Generator]
 * Web Site: http://www.hwelltech.com
 */

